package com.gdk.ksoft.ui.reporters;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.SystemData;
import com.gdk.ksoft.assembly.dialog.OrderStatementFindDialog;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnItemClick;

import java.util.ArrayList;

/**
 * Created by hel on 2014-11-21.
 * 报表页面
 */
public class FragmentReports extends Fragment {

    public static final String TAG = FragmentReports.class.getSimpleName();
    private static final String STR_LIST_ITEMS
            = "销售日报表,订单日报表,客户欠款查询,历史销售单查询,历史订单查询,开单查询,商品库存查询";
    private ArrayList<String> LIST_ITEMS = new ArrayList<>();

    @ViewInject(R.id.freports_lstReport)private ListView lstOrdersReport;//订单查询列表

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_reports,null);
        ViewUtils.inject(this, rootView);
        //处理列表项
        String[] listItems = STR_LIST_ITEMS.split(",");
        for(String item : listItems) LIST_ITEMS.add(item);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(SystemData.saleMan == null ||(SystemData.saleMan != null && SystemData.saleMan.cxqhqx == 1)){
            LIST_ITEMS.add("车销请货");//默认添加车销请货
        }
        lstOrdersReport.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.item_freports_lstreport,
                R.id.item_freports_lstreport_itemText,LIST_ITEMS));//设置列表项
    }

    /**
     * 是否能够查询订单
     * @return
     */
    public boolean isCanQueryOrder(){
        boolean isCan = false;
        if(SystemData.saleMan!=null)
            isCan = SystemData.saleMan.cxddts>0;
        if(!isCan) Toast.makeText(getActivity(),"对不起，您没有权限进行该项操作!",Toast.LENGTH_SHORT).show();
        return isCan;
    }

    /**
     * 列表项点击事件
     * @param av
     * @param view
     * @param position
     * @param itemId
     */
    @OnItemClick(value = {R.id.freports_lstReport})
    public void onItemClick(AdapterView<?> av,View view,int position,long itemId){
        switch(position){
            case 0://销售日报表
                if(!isCanQueryOrder()) return;//判断能否对单据信息进行查询
                new OrderStatementFindDialog(getActivity(),"销售日报查询","开始查询销售单",new OrderStatementFindDialog.OnExcuteQueryListener() {
                    @Override
                    public void onExcuteQuery(String dateString) {
                        Intent intent = new Intent(getActivity(),OrderSaleActivity.class);
                        intent.putExtra("dateToFind",dateString);
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                    }
                }).show();//显示查询框
                break;
            case 1://订单日报表
                if(!isCanQueryOrder()) return;//判断能否对单据信息进行查询
                new OrderStatementFindDialog(getActivity(),"订单日报查询","开始查询订单",new OrderStatementFindDialog.OnExcuteQueryListener() {
                    @Override
                    public void onExcuteQuery(String dateString) {
                        Intent intent = new Intent(getActivity(),OrderStatementActivity.class);
                        intent.putExtra("dateToFind",dateString);
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                    }
                }).show();//显示查询框
                break;
            case 2://客户欠款查询
                Intent debtIntent = new Intent(getActivity(),DebtStatementActivity.class);
                startActivity(debtIntent);
                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case 3://历史销售单查询
                if(!isCanQueryOrder()) return;//判断能否对单据信息进行查询
                Intent historyi = new Intent(getActivity(),HistoryOrderSaleActivity.class);
                startActivity(historyi);
                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case 4://历史订单查询
                if(!isCanQueryOrder()) return;//判断能否对单据信息进行查询
                Intent history = new Intent(getActivity(),HistoryOrderStatementActivity.class);
                startActivity(history);
                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case 5://开单查询
                if(!isCanQueryOrder()) return;//判断能否对单据信息进行查询
                Intent intentK = new Intent(getActivity(),AllOrderStatementActivity.class);
                startActivity(intentK);
                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case 6://库存查询
                Intent goodsStockIntent = new Intent(getActivity(),GoodsStockActivity.class);
                startActivity(goodsStockIntent);
                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
            case 7://车销请货
                Intent cxqhIntent = new Intent(getActivity(),CxqhActivity.class);
                startActivity(cxqhIntent);
                getActivity().overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                break;
        }
    }

}
